//
//  TextFieldCell.m
//  Guazi
//
//  Created by koolearn on 11-6-3.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "TextFieldCell.h"


#define kLeft 10
#define kHeight 20

@implementation TextFieldCell


@synthesize delegate;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        label = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:label];

        textField = [[UITextField alloc] initWithFrame:CGRectZero];
        textField.borderStyle = UITextBorderStyleNone;
 		textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
		textField.autocorrectionType = UITextAutocorrectionTypeNo;
       [self.contentView addSubview:textField];
    }
    return self;
}


- (void) updateWithDelegate:(id<TextFieldCellDelegate>)aDelegate tag:(NSInteger)tagNum
{
	self.delegate = aDelegate;
	tag = tagNum;
	
	label.text = [delegate label:tagNum];
	textField.text = [delegate text:tagNum];
	textField.placeholder = [delegate textFieldPlaceHolder:tagNum];
	textField.delegate = delegate;
	textField.tag = tagNum;
}


- (void) setSecurity:(BOOL)style
{
    if ( style ) {
        textField.secureTextEntry = YES;
    }
}


- (void) setKeybordType:(UIKeyboardType)type
{
    textField.keyboardType = type;
}


- (void) layoutSubviews
{
    CGFloat x = kLeft;
    CGFloat y = (self.bounds.size.height - kHeight) / 2;
    CGFloat w = [delegate labelWidth:tag];
    
    label.frame = CGRectMake( x, y, w, kHeight);

    x = kLeft + w + 5;
    w = self.bounds.size.width - 2 * kLeft - w - 5;
    textField.frame = CGRectMake( x, y, w, kHeight);
    
    [super layoutSubviews];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)dealloc
{
    [label release];
    [textField release];
    [(id)delegate release];
    [super dealloc];
}


- (void) showKeyboard
{
    [textField becomeFirstResponder];
}

@end
